001 /** 002 * Java Gui Builder - A library to build GUIs using an XML file. 003 * Copyright 2002, 2003 (C) François Beausoleil 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package jgb.factories.swing; 021 022 import jgb.builder.TagHandler; 023 import jgb.builder.TagHandlerFactory; 024 025 import java.util.HashMap; 026 import java.util.Map; 027 028 /** 029 * Implementation of {@link jgb.builder.TagHandlerFactory TagHandlerFactory} 030 * that returns {@link jgb.builder.TagHandler TagHandler}s based on 031 * a package name and class suffix. 032 * This factory will use the following formula to build the fully-qualified 033 * name of the class to instantiate: 034 * <ol> 035 * <li>The package name (prefix)</li> 036 * <li>a period (dot - ".")</li> 037 * <li>the first letter of the tag name, upper cased</li> 038 * <li>the remaining characters of the tag name</li> 039 * <li>the suffix</li> 040 * </ol> 041 * For example, if an instance of this factory was created with the package name 042 * equal to "com.xyz.handlers" and the suffix equal to "XmlTagHandler", 043 * {@link #getHandlerFor(java.lang.String) getHandlerFor(String)} would return 044 * an instance of <em>com.xyz.handlers.WindowsXmlTagHandler</em> if the tag name was 045 * equal to <em>windows</em>.<br /> 046 * This factory expects all {@link jgb.builder.TagHandler TagHandler}s 047 * to have a no-arg constructor. 048 * 049 * @since 0.1.1a 050 * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a> 051 */ 052 public class PackageTagHandlerFactory implements TagHandlerFactory { 053 private String basePackageName; 054 private String classSuffix; 055 private Map tagsMap = new HashMap(); 056 057 /** 058 * @param basePackageName A String that will be prepended to the final 059 * class name before instantiation. 060 * @param classSuffix A String that will be appended to the final class name 061 * before instantiation. 062 */ 063 public PackageTagHandlerFactory(final String basePackageName, final String classSuffix) { 064 if (basePackageName.endsWith(".")) { 065 this.basePackageName = basePackageName; 066 } else { 067 this.basePackageName = basePackageName + "."; 068 } 069 070 this.classSuffix = classSuffix; 071 } 072 073 public TagHandler getHandlerFor(final String tagName) throws ClassNotFoundException { 074 if (false == tagsMap.containsKey(tagName)) { 075 tagsMap.put(tagName, createTag(tagName)); 076 } 077 078 return (TagHandler)tagsMap.get(tagName); 079 } 080 081 private TagHandler createTag(final String tagName) throws ClassNotFoundException { 082 String className = buildClassName(tagName); 083 Class tagClass = Class.forName(className); 084 try { 085 return (TagHandler)tagClass.newInstance(); 086 } catch (Exception e) { 087 throw new ClassNotFoundException("Could not instantiate " + tagName, e); 088 } 089 } 090 091 private String buildClassName(final String tagName) { 092 StringBuffer classNameBuffer = new StringBuffer(); 093 classNameBuffer.append(basePackageName); 094 classNameBuffer.append(Character.toUpperCase(tagName.charAt(0))); 095 classNameBuffer.append(tagName.substring(1)); 096 classNameBuffer.append(classSuffix); 097 return classNameBuffer.toString(); 098 } 099 }